package com.zeng.jiudian.servlet.hotel;

import com.alibaba.fastjson.JSONObject;
import com.zeng.jiudian.bean.HotelModel;
import com.zeng.jiudian.service.HotelService;
import com.zeng.jiudian.service.impl.HotelServiceImpl;
import com.zeng.jiudian.utils.Upload;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Author JJ_yo
 * @Date 2020/7/28 - 9:48
 */
@WebServlet("/back/hotel/add")
public class HotelAddServlet extends HttpServlet
{
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
    {
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        resp.setContentType("application/json;charset=utf-8");

        Upload upload = new Upload();
        String result = upload.imgUpload(req);
        String[] results = result.split("@@");

        HotelModel hotelModel = new HotelModel();
        hotelModel.setContent(results[0]);
        hotelModel.setAddress(results[1]);
        hotelModel.setPhone(results[2]);
        hotelModel.setFax(results[3]);
        hotelModel.setEmail(results[4]);
        hotelModel.setWebsite(results[5]);
        hotelModel.setReservationPhone(results[6]);
        hotelModel.setQrCode(results[7]);
        hotelModel.setIsShow((results[8] == null || "".equals(results[8])) ? 0 : Integer.parseInt(results[8]));
        hotelModel.setRecommend((results[9] == null || "".equals(results[9])) ? 0 : Integer.parseInt(results[9]));

        HotelService hotelService = new HotelServiceImpl();
        int count = hotelService.add(hotelModel);

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("count", count);
        resp.getWriter().println(jsonObject);
    }
}
